<template>
    <el-dialog
            :title="!dataForm.accountId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="会计科目ID" prop="accountId">
                        <el-input v-model.number="dataForm.accountId" placeholder="会计科目ID"></el-input>
                </el-form-item>
                <el-form-item label="会计科目代码" prop="accountCode">
                        <el-input v-model="dataForm.accountCode" placeholder="会计科目代码"></el-input>
                </el-form-item>
                <el-form-item label="会计科目名称" prop="accountName">
                        <el-input v-model="dataForm.accountName" placeholder="会计科目名称"></el-input>
                </el-form-item>
                <el-form-item label="上级科目ID（用于明细科目）" prop="parentAccountId">
                        <el-input v-model.number="dataForm.parentAccountId" placeholder="上级科目ID（用于明细科目）"></el-input>
                </el-form-item>
                <el-form-item label="科目类别" prop="accountType">
                        <el-input v-model="dataForm.accountType" placeholder="科目类别"></el-input>
                </el-form-item>
                <el-form-item label="是否启用（true表示启用，false表示停用）" prop="isActive">
                        <el-input v-model="dataForm.isActive" placeholder="是否启用（true表示启用，false表示停用）"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            accountId: null,
            accountCode: null,
            accountName: null,
            parentAccountId: null,
            accountType: null,
            isActive: null
          },
          dataRule: {
            accountId: [
                        { required: true, message: '会计科目ID不能为空', trigger: 'blur' }
            ],
            accountCode: [
                        { required: true, message: '会计科目代码不能为空', trigger: 'blur' }
            ],
            accountName: [
                        { required: true, message: '会计科目名称不能为空', trigger: 'blur' }
            ],
            parentAccountId: [
                        { required: true, message: '上级科目ID（用于明细科目）不能为空', trigger: 'blur' }
            ],
            accountType: [
                        { required: true, message: '科目类别不能为空', trigger: 'blur' }
            ],
            isActive: [
                        { required: true, message: '是否启用（true表示启用，false表示停用）不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.accountId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.accountId = id || 0
            if (this.dataForm.accountId) {
              this.$http({
                url: this.$http.adornUrl(`/app/accounts/info/${this.dataForm.accountId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appaccounts
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/accounts/${!this.dataForm.accountId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>